<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Report extends CI_Controller {
            public function __construct(){
                        parent::__construct();
                        $this->load->model(array("commonmodel"));
                      
                }

  public function index()
  {
    
    $data=array();
    
    $products=$this->commonmodel->get_products();
    $data['products']=$products;
    load($this->db,$this->session,$this->load,"report","index",$data);
  }


public function readid(){
  $departstr=depart_tree_result($this->db,$this->session->userdata('depart'));
   echo $departstr;
}

   public function depart()
  {
    
    $data=array();
    $products=$this->commonmodel->get_products();
    $data['products']=$products;
    load($this->db,$this->session,$this->load,"report","depart",$data);
  }



    





public function get_depart_dao(){
                date_default_timezone_set('Asia/Shanghai');
                header("Content-Type: text/csv");
                $date=@date("Y-m-d",time()).".csv";
                header("Content-Disposition: attachment; filename=".$date);
                header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
                header('Expires:0');
                header('Pragma:public');
                  $starttime=@strtotime($this->input->get("starttime"));
                      $endtime=@strtotime($this->input->get("endtime"));
                   $departstr=depart_tree_result($this->db,$this->session->userdata('depart'));
                   $sql="select id,name,cust_no from employee where depart in ($departstr) ";
                   $query=$this->db->query($sql);
                   $employees=$query->result_array();
                   foreach ($employees as $key => $value) {
                    $empid=$value['id'];
                     $sql="select product_id,sum(total) as total  from order_to_product where empid=$empid and (time between $starttime and $endtime) group by product_id";
                     $query=$this->db->query($sql);
                     $arr=$query->result_array();
                     foreach ($arr as $key2 => $value2) {
                      $product_id=$value2['product_id'];
                      $total=$value2['total'];
                       $employees[$key][$product_id]=$total; 
                     }
                   }

                   $sql="select id,name from point order by id ASC";
                   $query=$this->db->query($sql);
                   $points=$query->result_array();
                   $numarr=array();
                   foreach ($points as $key => $value) {
                     $numarr[]=$value['id'];
                   }

                   $tmp=array();
                 
                   foreach ($employees as $key => $value) {
                    foreach ($value as $key2 => $value2) {
                     $tmp[]=$key2;
                    }
                     
                      $test=array_diff($numarr,$tmp);
                      foreach ($test as $key3 => $value3) {
                        $employees[$key][$value3]="";
                      }
                      ksort($employees[$key]);
                      unset($employees[$key]['id']);
                      $tmp=array();
                   }
                 
                  $title="员工编号,员工名称,";
                  foreach ($points as $key => $value) {
                    $title=$title.$value['name'].",";
                  }
                  $title=$title."\r\n";
                  echo $title;
                  $str="";
                  foreach ($employees as $key => $value) {
                    foreach ($value as $key => $value) {
                      $str=$str.$value.",";
                    }
                    $str=$str."\r\n";
                  }
                  
                  echo $str;
                 

}






public function get_all_dao(){
date_default_timezone_set('Asia/Shanghai');
                header("Content-Type: text/csv");
                $date=@date("Y-m-d",time()).".csv";
                header("Content-Disposition: attachment; filename=".$date);
                header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
                header('Expires:0');
                header('Pragma:public');
                  $starttime=@strtotime($this->input->get("starttime"));
                      $endtime=@strtotime($this->input->get("endtime"));
                   
                   $sql="select id,name,cust_no from employee  ";
                   $query=$this->db->query($sql);
                   $employees=$query->result_array();
                   foreach ($employees as $key => $value) {
                    $empid=$value['id'];
                     $sql="select product_id,sum(total) as total  from order_to_product where empid=$empid and (time between $starttime and $endtime) group by product_id";
                     $query=$this->db->query($sql);
                     $arr=$query->result_array();
                     foreach ($arr as $key2 => $value2) {
                      $product_id=$value2['product_id'];
                      $total=$value2['total'];
                       $employees[$key][$product_id]=$total; 
                     }
                   }

                   $sql="select id,name from point order by id ASC";
                   $query=$this->db->query($sql);
                   $points=$query->result_array();
                   $numarr=array();
                   foreach ($points as $key => $value) {
                     $numarr[]=$value['id'];
                   }

                   $tmp=array();
                 
                   foreach ($employees as $key => $value) {
                    foreach ($value as $key2 => $value2) {
                     $tmp[]=$key2;
                    }
                     
                      $test=array_diff($numarr,$tmp);
                      foreach ($test as $key3 => $value3) {
                        $employees[$key][$value3]="";
                      }
                      ksort($employees[$key]);
                      unset($employees[$key]['id']);
                      $tmp=array();
                   }
                 
                  $title="员工编号,员工名称,";
                  foreach ($points as $key => $value) {
                    $title=$title.$value['name'].",";
                  }
                  $title=$title."\r\n";
                  echo $title;
                  $str="";
                  foreach ($employees as $key => $value) {
                    foreach ($value as $key => $value) {
                      $str=$str.$value.",";
                    }
                    $str=$str."\r\n";
                  }
                  
                  echo $str;
                 
}





public function get_depart_score(){
  $starttime=@strtotime($this->input->get("starttime"));
    $endtime=@strtotime($this->input->get("endtime"));
  $departstr=depart_tree_result($this->db,$this->session->userdata('depart'));
 $sql="select name,id,cust_no from employee where id in ($departstr)";
 $query=$this->db->query($sql);
 $employees=$query->result_array();
 foreach ($employees as $key => $value) {
  $empid=$value['id'];
   $sql="select product_id,sum(total) as total  from order_to_product where empid=$empid and (time between $starttime and $endtime) group by product_id";
   $query=$this->db->query($sql);
   $arr=$query->result_array();
   foreach ($arr as $key2 => $value2) {
    $product_id=$value2['product_id'];
    $total=$value2['total'];
     $employees[$key][$product_id]=$total; 
   }
 }
 $str=json_encode($employees);
 echo $str;
}



















public function get_all_score(){
 $starttime=@strtotime($this->input->get("starttime"));
    $endtime=@strtotime($this->input->get("endtime"));
 $sql="select name,id,cust_no from employee ";
 $query=$this->db->query($sql);
 $employees=$query->result_array();
 foreach ($employees as $key => $value) {
  $empid=$value['id'];
   $sql="select product_id,sum(total) as total  from order_to_product where empid=$empid  and (time between $starttime and $endtime) group by product_id";
   $query=$this->db->query($sql);
   $arr=$query->result_array();
   foreach ($arr as $key2 => $value2) {
    $product_id=$value2['product_id'];
    $total=$value2['total'];
     $employees[$key][$product_id]=$total; 
   }
 }
 $str=json_encode($employees);
 echo $str;
}

  public function del(){
            
    $id = intval($_REQUEST['id']);
    $tmp=0; 

   }


}
